public class Q2 {

  public static class ListNode {

     public int value;

     public ListNode next;
  }
 
  public static boolean hasLoops( ListNode myList ) {
      // Trivial case.
      if (null == myList) {
          return false;
      }
      
      ListNode slow = myList, fast = myList;
      
      while (true) {
          slow = slow.next;
          if (null != fast.next) {
              fast = fast.next.next;
          } else { // fast reaches end
              return false;
          }
          
          // Either runner reaches null.
          if (fast == null || slow == null) {
              return false;
          }
          
          // They meet, loop detected.
          if (fast == slow) {
              return true;
          }
      }
      
  }
  
  public static void main(String[] args) {
	  int n = 10;
	ListNode head = new ListNode();
	head.value = -1;
	ListNode x = head;
	for (int i = 0; i < n; i++) {
		x.next = new ListNode();
		x.next.value = i;
		x = x.next;
	}
//	x.next = head;
	
	System.out.println(hasLoops(head));
}
}